import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { bindGuards } from './guard';

/**
 * customLayout 自定义layout
 * meta:{
 *      customLayout:
 * }
 */
const routes: Array<RouteRecordRaw> = [
    {
        path: '/unauthorized',
        component: () => import('../views/unauthorized.vue'),
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/user/login.vue'),
    },
    {
        path: '/diagnose/record',
        name: 'diagnoseRecord',
        component: () => import('../views/diagnose/record.vue'),
    },
    {
        path: '/diagnose/report',
        name: 'diagnoseReport',
        component: () => import('../views/diagnose/report.vue'),
    },
    {
        // :pathMatch(.*)* 是一个参数，正则表达式 (.*)* 会匹配任何路径
        path: '/:pathMatch(.*)*',
        name: '404',
        component: () => import('../views/404.vue'),
    },
];

async function importCommonConfig() {
    const res = await import('../../config/index.js' as unknown as string);
    return res;
}
const curEnv = process.env.APP_DEPLOY_ENV as string;

const curConfig = await importCommonConfig();
const router = createRouter({
    history: createWebHistory(curConfig[curEnv]?.baseURL),
    routes,
    scrollBehavior() {
        return { top: 0 };
    },
});

bindGuards(router);

export default router;
